package main

import (
	"fmt"
	"sync"
)

func main()  {
	//函数只能调用一次

	//示例：
	var count int
	increment := func() {
		count++
	}
	var once sync.Once

	var increments sync.WaitGroup
	increments.Add(100)
	for i:=0;i<100;i++{
		go func() {
			defer increments.Done()
			increment()
			once.Do(increment)
		}()
	}

	increments.Wait()
	fmt.Printf("Count is %d\n",count)


	syncOnce()
}

func syncOnce()  {
	//sync.Once只计算Do方法的次数
	//示例：
	var count int
	increment := func() { count++ }
	decrement := func() { count-- }

	var once sync.Once
	once.Do(increment)
	once.Do(decrement)
	fmt.Printf("Count: %d\n",count)
}

